#include "tubeface.h"

#include <stdio.h>

#include "graphics.h"
#include "model.h"
#include "image.h"
#include "timer.h"
#include "rammel.h"

#include "tubeface.xbm"

static pixel_t face_pix[tubeface_width * tubeface_height];
static image_t face_tex = {
    .data = face_pix,
    .width = tubeface_width,
    .height = tubeface_height
};

static const model_t model_tubeface = {
    .vertex_count = 208,
    .vertices = (vertex_t*)(float[][5]){
        {-7.5002, 11.2248, -6, 0.09375, 0.6082}, {-7.5002, 11.2248, 32, 0.09375, 1}, {-5.16623, 12.4724, 32, 0.0625, 1}, {-5.16623, 12.4724, -6, 0.0625, 0.6082}, {2.63372, 13.2406, -6, 0.96875, 0.6082}, {2.63372, 13.2406, 32, 0.96875, 1}, {5.16623, 12.4724, 32, 0.9375, 1}, {5.16623, 12.4724, -6, 0.9375, 0.6082}, 
        {0, -15.5778, 13, 0.5, 0.8041}, {2.63372, -14.0736, 13, 0.53125, 0.8041}, {2.63372, -13.2406, 16.8, 0.53125, 0.84328}, {0, -14.6056, 16.8, 0.5, 0.84328}, {9.54594, -9.54594, 5.4, 0.625, 0.72574}, {9.54594, -9.54594, 16.8, 0.625, 0.84328}, {7.5002, -11.2248, 16.8, 0.59375, 0.84328}, {7.5002, -11.2248, 13, 0.59375, 0.8041}, 
        {7.5002, -11.2248, 9.2, 0.59375, 0.76492}, {7.5002, -11.2248, 5.4, 0.59375, 0.72574}, {7.5002, -11.2248, 20.6, 0.59375, 0.88246}, {7.5002, -11.2248, 24.4, 0.59375, 0.92164}, {7.5002, -11.2248, 32, 0.59375, 1}, {5.16623, -12.4724, 32, 0.5625, 1}, {5.16623, -12.4724, 16.8, 0.5625, 0.84328}, {5.16623, -12.4724, 13, 0.5625, 0.8041}, 
        {0, -13.5, 20.6, 0.5, 0.88246}, {0, -13.5, 32, 0.5, 1}, {-2.63372, -13.2406, 32, 0.46875, 1}, {-2.63372, -13.2406, 20.6, 0.46875, 0.88246}, {11.2248, -7.5002, 24.4, 0.65625, 0.92164}, {12.4724, -5.16622, 24.4, 0.6875, 0.92164}, {12.4724, -5.16622, 32, 0.6875, 1}, {11.2248, -7.5002, 32, 0.65625, 1}, 
        {13.2406, 2.63372, -6, 0.78125, 0.6082}, {13.2406, 2.63372, 32, 0.78125, 1}, {13.5, 2e-06, 32, 0.75, 1}, {13.5, -0, -6, 0.75, 0.6082}, {9.54594, -9.54594, 24.4, 0.625, 0.92164}, {9.54594, -9.54594, 32, 0.625, 1}, {-12.4724, -5.16622, 20.6, 0.3125, 0.88246}, {-12.4724, -5.16622, 24.4, 0.3125, 0.92164}, 
        {-12.4724, -5.16622, 32, 0.3125, 1}, {-13.2406, -2.63372, 32, 0.28125, 1}, {-13.2406, -2.63372, -6, 0.28125, 0.6082}, {-12.4724, -5.16623, -6, 0.3125, 0.6082}, {-12.4724, -5.16622, 16.8, 0.3125, 0.84328}, {-13.5, -0, -6, 0.25, 0.6082}, {-13.5, 2e-06, 32, 0.25, 1}, {-13.2406, 2.63372, 32, 0.21875, 1}, 
        {-13.2406, 2.63372, -6, 0.21875, 0.6082}, {-11.2248, -7.5002, 24.4, 0.34375, 0.92164}, {-11.2248, -7.5002, 32, 0.34375, 1}, {0, -1e-06, -13.3244, 0, 0}, {12.4724, -5.16623, -6, 0.6875, 0.6082}, {11.2248, -7.5002, -6, 0.65625, 0.6082}, {13.2406, -2.63372, -6, 0.71875, 0.6082}, {-2.63372, -13.2406, -6, 0.46875, 0.6082}, 
        {-5.16623, -12.4724, -6, 0.4375, 0.6082}, {12.4724, 5.16623, -6, 0.8125, 0.6082}, {11.2248, 7.5002, -6, 0.84375, 0.6082}, {9.54594, -9.54594, -6, 0.625, 0.6082}, {0, -13.5, -6, 0.5, 0.6082}, {-7.5002, -11.2248, -6, 0.40625, 0.6082}, {9.54594, 9.54594, -6, 0.875, 0.6082}, {7.5002, 11.2248, -6, 0.90625, 0.6082}, 
        {2.63372, -13.2406, -6, 0.53125, 0.6082}, {0, 13.5, -32.0012, 0, 0.209251}, {0, 13.5, -21.0267, 0, 0.359157}, {2.63372, 13.2406, -21.0267, 0.03125, 0.359157}, {2.63372, 13.2406, -32.0012, 0.03125, 0.209251}, {5.16623, 12.4724, -21.0267, 0.0625, 0.359157}, {5.16623, 12.4724, -32.0012, 0.0625, 0.209251}, {7.5002, 11.2248, -21.0267, 0.09375, 0.359157}, 
        {7.5002, 11.2248, -32.0012, 0.09375, 0.209251}, {9.54594, 9.54594, -21.0267, 0.125, 0.359157}, {9.54594, 9.54594, -32.0012, 0.125, 0.209251}, {11.2248, 7.5002, -21.0267, 0.15625, 0.359157}, {11.2248, 7.5002, -32.0012, 0.15625, 0.209251}, {12.4724, 5.16622, -21.0267, 0.1875, 0.359157}, {12.4724, 5.16622, -32.0012, 0.1875, 0.209251}, {13.2406, 2.63372, -21.0267, 0.21875, 0.359157}, 
        {13.2406, 2.63372, -32.0012, 0.21875, 0.209251}, {13.5, -1e-06, -21.0267, 0.25, 0.359157}, {13.5, -2e-06, -32.0012, 0.25, 0.209251}, {13.2406, -2.63372, -21.0267, 0.28125, 0.359157}, {13.2406, -2.63372, -32.0012, 0.28125, 0.209251}, {12.4724, -5.16623, -21.0267, 0.3125, 0.359157}, {12.4724, -5.16623, -32.0012, 0.3125, 0.209251}, {11.2248, -7.5002, -21.0267, 0.34375, 0.359157}, 
        {11.2248, -7.5002, -32.0012, 0.34375, 0.209251}, {9.54594, -9.54594, -21.0267, 0.375, 0.359157}, {9.54594, -9.54594, -32.0012, 0.375, 0.209251}, {7.5002, -11.2248, -21.0267, 0.40625, 0.359157}, {7.5002, -11.2248, -32.0012, 0.40625, 0.209251}, {5.16623, -12.4724, -21.0267, 0.4375, 0.359157}, {5.16623, -12.4724, -32.0012, 0.4375, 0.209251}, {2.63372, -13.2406, -21.0267, 0.46875, 0.359157}, 
        {2.63372, -13.2406, -32.0012, 0.46875, 0.209251}, {0, -13.5, -21.0267, 0.5, 0.359157}, {0, -13.5, -32.0012, 0.5, 0.209251}, {-2.63372, -13.2406, -21.0267, 0.53125, 0.359157}, {-2.63372, -13.2406, -32.0012, 0.53125, 0.209251}, {-5.16623, -12.4724, -21.0267, 0.5625, 0.359157}, {-5.16623, -12.4724, -32.0012, 0.5625, 0.209251}, {-7.5002, -11.2248, -21.0267, 0.59375, 0.359157}, 
        {-7.5002, -11.2248, -32.0012, 0.59375, 0.209251}, {-9.54594, -9.54594, -21.0267, 0.625, 0.359157}, {-9.54594, -9.54594, -32.0012, 0.625, 0.209251}, {-11.2248, -7.5002, -21.0267, 0.65625, 0.359157}, {-11.2248, -7.5002, -32.0012, 0.65625, 0.209251}, {-12.4724, -5.16623, -21.0267, 0.6875, 0.359157}, {-12.4724, -5.16623, -32.0012, 0.6875, 0.209251}, {-13.2406, -2.63372, -21.0267, 0.71875, 0.359157}, 
        {-13.2406, -2.63372, -32.0012, 0.71875, 0.209251}, {-13.5, -1e-06, -21.0267, 0.75, 0.359157}, {-13.5, -2e-06, -32.0012, 0.75, 0.209251}, {-13.2406, 2.63372, -21.0267, 0.78125, 0.359157}, {-13.2406, 2.63372, -32.0012, 0.78125, 0.209251}, {-12.4724, 5.16622, -21.0267, 0.8125, 0.359157}, {-12.4724, 5.16622, -32.0012, 0.8125, 0.209251}, {-11.2248, 7.5002, -21.0267, 0.84375, 0.359157}, 
        {-11.2248, 7.5002, -32.0012, 0.84375, 0.209251}, {-9.54594, 9.54594, -21.0267, 0.875, 0.359157}, {-9.54594, 9.54594, -32.0012, 0.875, 0.209251}, {-7.5002, 11.2248, -21.0267, 0.90625, 0.359157}, {-7.5002, 11.2248, -32.0012, 0.90625, 0.209251}, {-5.16623, 12.4724, -21.0267, 0.9375, 0.359157}, {-5.16623, 12.4724, -32.0012, 0.9375, 0.209251}, {-2.63372, 13.2406, -21.0267, 0.96875, 0.359157}, 
        {-2.63372, 13.2406, -32.0012, 0.96875, 0.209251}, {0, 13.5, -21.0267, 1, 0.359157}, {0, 13.5, -32.0012, 1, 0.209251}, {7.5002, -11.2248, -6, 0.59375, 0.6082}, {5.16623, -12.4724, -6, 0.5625, 0.6082}, {0, 13.5, -6, 0, 0.6082}, {-2.63372, 13.2406, -6, 0.03125, 0.6082}, {-2.63372, 13.2406, 32, 0.03125, 1}, 
        {0, 13.5, 32, 0, 1}, {-12.4724, 5.16623, 32, 0.1875, 1}, {-12.4724, 5.16623, -6, 0.1875, 0.6082}, {-7.5002, -11.2248, 9.2, 0.40625, 0.76492}, {-7.5002, -11.2248, 13, 0.40625, 0.8041}, {-7.5002, -11.2248, 16.8, 0.40625, 0.84328}, {-9.54594, -9.54594, 16.8, 0.375, 0.84328}, {-9.54594, -9.54594, 5.4, 0.375, 0.72574}, 
        {-7.5002, -11.2248, 5.4, 0.40625, 0.72574}, {7.5002, 11.2248, 32, 0.90625, 1}, {9.54594, 9.54594, 32, 0.875, 1}, {-11.2248, 7.5002, -6, 0.15625, 0.6082}, {-11.2248, 7.5002, 32, 0.15625, 1}, {-9.54594, 9.54594, 32, 0.125, 1}, {-9.54594, 9.54594, -6, 0.125, 0.6082}, {-11.2248, -7.5002, 16.8, 0.34375, 0.84328}, 
        {-10.1275, -6.84764, 20.634, 0.34375, 0.88246}, {-9.54594, -9.54594, 24.4, 0.375, 0.92164}, {-9.54594, -9.54594, 32, 0.375, 1}, {-8.44856, -8.89338, 20.634, 0.375, 0.88246}, {-9.54594, -9.54594, -6, 0.375, 0.6082}, {-7.5002, -11.2248, -2.2, 0.40625, 0.64738}, {-9.54594, -9.54594, -2.2, 0.375, 0.64738}, {-7.6284, -12.3236, 1.6, 0.40625, 0.68656}, 
        {-9.54594, -9.54594, 1.6, 0.375, 0.68656}, {-11.2248, -7.5002, -6, 0.34375, 0.6082}, {-7.5002, -11.2248, 20.6, 0.40625, 0.88246}, {-7.5002, -11.2248, 24.4, 0.40625, 0.92164}, {-5.16623, -12.4724, -2.2, 0.4375, 0.64738}, {-5.29443, -13.5711, 1.6, 0.4375, 0.68656}, {-5.16623, -12.4724, 5.4, 0.4375, 0.72574}, {-5.16623, -13.9468, 9.2, 0.4375, 0.76492}, 
        {-5.16623, -12.4724, 13, 0.4375, 0.8041}, {-7.5002, -11.2248, 32, 0.40625, 1}, {-5.16623, -12.4724, 16.8, 0.4375, 0.84328}, {-5.16623, -12.4724, 32, 0.4375, 1}, {-2.63372, -13.2406, -2.2, 0.46875, 0.64738}, {-2.76192, -14.3394, 1.6, 0.46875, 0.68656}, {-2.63372, -13.2406, 5.4, 0.46875, 0.72574}, {-2.63372, -16.1945, 9.2, 0.46875, 0.76492}, 
        {-2.63372, -14.0736, 13, 0.46875, 0.8041}, {-2.63372, -13.2406, 16.8, 0.46875, 0.84328}, {0, -13.5, -2.2, 0.5, 0.64738}, {0, -14.7908, 1.6, 0.5, 0.68656}, {0, -13.5, 5.4, 0.5, 0.72574}, {0, -16.7239, 9.2, 0.5, 0.76492}, {2.63372, -13.2406, 20.6, 0.53125, 0.88246}, {2.63372, -13.2406, 32, 0.53125, 1}, 
        {2.63372, -13.2406, -2.2, 0.53125, 0.64738}, {2.76192, -14.3394, 1.6, 0.53125, 0.68656}, {2.63372, -13.2406, 5.4, 0.53125, 0.72574}, {2.63372, -16.1945, 9.2, 0.53125, 0.76492}, {5.16623, -12.4724, -2.2, 0.5625, 0.64738}, {5.29443, -13.5711, 1.6, 0.5625, 0.68656}, {5.16623, -12.4724, 5.4, 0.5625, 0.72574}, {5.16623, -13.9468, 9.2, 0.5625, 0.76492}, 
        {7.5002, -11.2248, -2.2, 0.59375, 0.64738}, {7.6284, -12.3236, 1.6, 0.59375, 0.68656}, {12.4724, -5.16622, 16.8, 0.6875, 0.84328}, {11.2248, -7.5002, 16.8, 0.65625, 0.84328}, {9.54594, -9.54594, -2.2, 0.625, 0.64738}, {9.54594, -9.54594, 1.6, 0.625, 0.68656}, {8.44856, -8.89338, 20.634, 0.625, 0.88246}, {10.1275, -6.84764, 20.634, 0.65625, 0.88246}, 
        {12.4724, -5.16622, 20.6, 0.6875, 0.88246}, {13.2406, -2.63372, 32, 0.71875, 1}, {11.2248, 7.5002, 32, 0.84375, 1}, {12.4724, 5.16623, 32, 0.8125, 1}, {0, 13.5, -6, 1, 0.6082}, {0, 13.5, 32, 1, 1}, {0, 13.5, -6, 0, 0.6082}, {0, -1e-06, -13.7023, 0, 0}, 
    },
    .surface_count = 1,
    .surfaces = (surface_t[]){
        {1014, (index_t[]){
            1, 2, 3, 1, 3, 0, 5, 6, 7, 5, 7, 4, 9, 10, 11, 9, 11, 8, 13, 14, 15, 13, 15, 16, 13, 16, 17, 13, 17, 12, 19, 20, 21, 19, 21, 22, 19, 22, 23, 19, 
            23, 15, 19, 15, 14, 19, 14, 18, 25, 26, 27, 25, 27, 24, 29, 30, 31, 29, 31, 28, 33, 34, 35, 33, 35, 32, 28, 31, 37, 28, 37, 36, 39, 40, 41, 39, 41, 42, 39, 42, 
            43, 39, 43, 44, 39, 44, 38, 46, 47, 48, 46, 48, 45, 49, 50, 40, 49, 40, 39, 52, 53, 51, 54, 52, 51, 35, 54, 51, 32, 35, 51, 55, 56, 51, 57, 32, 51, 58, 57, 51, 
            53, 59, 51, 60, 55, 51, 56, 61, 51, 62, 58, 51, 63, 62, 51, 64, 60, 51, 66, 67, 68, 66, 68, 65, 67, 69, 70, 67, 70, 68, 69, 71, 72, 69, 72, 70, 71, 73, 74, 71, 
            74, 72, 73, 75, 76, 73, 76, 74, 75, 77, 78, 75, 78, 76, 77, 79, 80, 77, 80, 78, 79, 81, 82, 79, 82, 80, 81, 83, 84, 81, 84, 82, 83, 85, 86, 83, 86, 84, 85, 87, 
            88, 85, 88, 86, 87, 89, 90, 87, 90, 88, 89, 91, 92, 89, 92, 90, 91, 93, 94, 91, 94, 92, 93, 95, 96, 93, 96, 94, 95, 97, 98, 95, 98, 96, 97, 99, 100, 97, 100, 98, 
            99, 101, 102, 99, 102, 100, 101, 103, 104, 101, 104, 102, 103, 105, 106, 103, 106, 104, 105, 107, 108, 105, 108, 106, 107, 109, 110, 107, 110, 108, 109, 111, 112, 109, 112, 110, 111, 113, 114, 111, 
            114, 112, 113, 115, 116, 113, 116, 114, 115, 117, 118, 115, 118, 116, 117, 119, 120, 117, 120, 118, 119, 121, 122, 119, 122, 120, 121, 123, 124, 121, 124, 122, 123, 125, 126, 123, 126, 124, 125, 127, 
            128, 125, 128, 126, 127, 129, 130, 127, 130, 128, 7, 63, 51, 4, 7, 51, 131, 132, 51, 59, 131, 51, 133, 4, 51, 132, 64, 51, 135, 136, 133, 135, 133, 134, 2, 135, 134, 2, 134, 3, 
            47, 137, 138, 47, 138, 48, 140, 141, 142, 140, 142, 143, 140, 143, 144, 140, 144, 139, 145, 146, 62, 145, 62, 63, 148, 149, 150, 148, 150, 147, 151, 152, 38, 151, 38, 44, 152, 49, 39, 152, 
            39, 38, 153, 154, 50, 153, 50, 49, 142, 155, 152, 142, 152, 151, 155, 153, 49, 155, 49, 152, 61, 157, 158, 61, 158, 156, 157, 159, 160, 157, 160, 158, 159, 144, 143, 159, 143, 160, 142, 151, 
            161, 142, 161, 156, 142, 156, 158, 142, 158, 160, 142, 160, 143, 141, 162, 155, 141, 155, 142, 162, 163, 153, 162, 153, 155, 56, 164, 157, 56, 157, 61, 164, 165, 159, 164, 159, 157, 165, 166, 144, 
            165, 144, 159, 166, 167, 139, 166, 139, 144, 167, 168, 140, 167, 140, 139, 163, 169, 154, 163, 154, 153, 170, 171, 169, 170, 169, 163, 170, 163, 162, 170, 162, 141, 170, 141, 140, 170, 140, 168, 55, 
            172, 164, 55, 164, 56, 172, 173, 165, 172, 165, 164, 173, 174, 166, 173, 166, 165, 174, 175, 167, 174, 167, 166, 175, 176, 168, 175, 168, 167, 23, 22, 10, 23, 10, 9, 27, 26, 171, 27, 171, 
            170, 27, 170, 177, 60, 178, 172, 60, 172, 55, 178, 179, 173, 178, 173, 172, 179, 180, 174, 179, 174, 173, 180, 181, 175, 180, 175, 174, 181, 8, 176, 181, 176, 175, 11, 24, 27, 11, 27, 177, 
            183, 25, 24, 183, 24, 182, 64, 184, 178, 64, 178, 60, 184, 185, 179, 184, 179, 178, 185, 186, 180, 185, 180, 179, 186, 187, 181, 186, 181, 180, 187, 9, 8, 187, 8, 181, 10, 182, 24, 10, 
            24, 11, 132, 188, 184, 132, 184, 64, 188, 189, 185, 188, 185, 184, 189, 190, 186, 189, 186, 185, 190, 191, 187, 190, 187, 186, 191, 23, 9, 191, 9, 187, 21, 183, 182, 21, 182, 10, 21, 10, 
            22, 131, 192, 188, 131, 188, 132, 192, 193, 189, 192, 189, 188, 193, 17, 190, 193, 190, 189, 17, 16, 191, 17, 191, 190, 16, 15, 23, 16, 23, 191, 36, 37, 20, 36, 20, 19, 194, 195, 53, 
            194, 53, 52, 59, 196, 192, 59, 192, 131, 196, 197, 193, 196, 193, 192, 197, 12, 17, 197, 17, 193, 13, 198, 18, 13, 18, 14, 198, 36, 19, 198, 19, 18, 195, 199, 198, 195, 198, 13, 199, 
            28, 36, 199, 36, 198, 149, 1, 0, 149, 0, 150, 161, 151, 44, 161, 44, 43, 194, 200, 199, 194, 199, 195, 200, 29, 28, 200, 28, 199, 201, 30, 29, 201, 29, 200, 201, 200, 194, 201, 194, 
            52, 201, 52, 54, 137, 148, 147, 137, 147, 138, 34, 201, 54, 34, 54, 35, 195, 13, 12, 195, 12, 197, 195, 197, 196, 195, 196, 59, 195, 59, 53, 202, 203, 57, 202, 57, 58, 146, 202, 58, 
            146, 58, 62, 203, 33, 32, 203, 32, 57, 6, 145, 63, 6, 63, 7, 41, 46, 45, 41, 45, 42, 205, 5, 4, 205, 4, 204, 61, 156, 51, 156, 161, 51, 161, 43, 51, 43, 42, 51, 42, 
            45, 51, 45, 48, 51, 48, 138, 51, 138, 147, 51, 147, 150, 51, 150, 0, 51, 0, 3, 51, 3, 134, 51, 134, 206, 51, 8, 11, 177, 8, 177, 176, 176, 177, 170, 176, 170, 168, 129, 207, 
            67, 67, 207, 69, 69, 207, 71, 71, 207, 73, 73, 207, 75, 75, 207, 77, 77, 207, 79, 79, 207, 81, 81, 207, 83, 83, 207, 85, 85, 207, 87, 87, 207, 89, 89, 207, 91, 91, 207, 93, 
            93, 207, 95, 95, 207, 97, 97, 207, 99, 99, 207, 101, 101, 207, 103, 103, 207, 105, 105, 207, 107, 107, 207, 109, 109, 207, 111, 111, 207, 113, 113, 207, 115, 115, 207, 117, 117, 207, 119, 119, 
            207, 121, 121, 207, 123, 123, 207, 125, 125, 207, 127, 127, 207, 129, 
        }, HEXPIX(FFFFFF), &face_tex}
    }
};

static uint8_t tubeface_shine = 0;


static uint32_t prbs31(uint32_t curr) {
    uint32_t next = (~((curr<<1)^(curr<<4)) & 0xFFFFFFF0);
    next |= (~(( ((curr<<1) & 0x0E) | ((next>>31) & 0x01)) ^ (next>>28)) & 0x0000000F);
    return next;
}

static void draw_voxel(pixel_t* volume, const int* coordinate, const float* barycentric, const triangle_state_t* triangle) {
    static uint32_t fuzz = 0;
    fuzz = prbs31(fuzz);

    pixel_t colour = triangle->colour;

    if (triangle->texture) {
        float texcoord[2] = {
            triangle->texcoord[0][0] * barycentric[0] + triangle->texcoord[1][0] * barycentric[1] + triangle->texcoord[2][0] * barycentric[2],
            triangle->texcoord[0][1] * barycentric[0] + triangle->texcoord[1][1] * barycentric[1] + triangle->texcoord[2][1] * barycentric[2]
        };
        
        bool masked = false;
        colour = image_sample(triangle->texture, texcoord, &masked);
        if (masked) {
            return;
        }
    }

    uint8_t shine = tubeface_shine + ((colour & 0x7f)*2);// + ((coordinate[0]^coordinate[1]^coordinate[2])&4)*109;

    int32_t x = coordinate[0] +  ((int)min((fuzz>>4)&0xff, 255-shine)*((fuzz&1)?1:-1)) / 80;
    int32_t y = coordinate[1] +  ((int)min((fuzz>>12)&0xff, shine)*((fuzz&1)?1:-1)) / 80;
    int32_t z = coordinate[2];
    if ((uint32_t)x >= VOXELS_X || (uint32_t)y >= VOXELS_Y || (uint32_t)z >= VOXELS_Z) {
        return;
    }

    uint8_t face = colour & 0x80;
    volume[VOXEL_INDEX(x, y, z)] = face ? HEXPIX(AA5555) : RGBPIX(shine, 0xAA, 0xFF);
}

void tubeface_init(void) {

    for (int y = 0; y < tubeface_height; ++y) {
        for (int x = 0; x < tubeface_width; ++x) {
            bool face = ((tubeface_bits[(x + y * tubeface_width) / 8] >> (x&7)) & 1) != 0;
            face_pix[x + y * tubeface_width] = ((x*4*128/tubeface_width)&0x7f) | (face?128:0);
        }
    }
}

void tubeface_draw(pixel_t* volume) {
    float matrix[MAT4_SIZE];

    tubeface_shine = timer_frame_time / 4;
    graphics_triangle_shader_cb = draw_voxel;

    mat4_identity(matrix);
    mat4_translation(matrix, matrix, (float[3]){VOXELS_X*0.5f, VOXELS_Y*0.5f, 32});
    model_draw(volume, &model_tubeface, matrix);

    graphics_triangle_shader_cb = NULL;
}
